package com.txsj.scan.presenter;

import android.text.TextUtils;

import com.txsj.scan.base.BasePresenter;
import com.txsj.scan.bean.CodeBean;
import com.txsj.scan.bean.LoginV2Bean;
import com.txsj.scan.bean.VerifyCodeBean;
import com.txsj.scan.http.ApiServer;
import com.txsj.scan.http.requestframework.HttpSign;
import com.txsj.scan.http.requestframework.RetrofitManager;
import com.txsj.scan.utils.MD5Util;
import com.txsj.scan.utils.PreferenceUtil;
import com.txsj.scan.utils.TextUtil;
import com.txsj.scan.utils.Trace;
import com.txsj.scan.view.ILoginView;

import java.util.HashMap;
import java.util.Map;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class LoginPresenter extends BasePresenter<ILoginView> {

    public LoginPresenter(ILoginView view) {
        super(view);
    }

    @Override
    protected void initModel() {

    }

    /**
     * 验证手机号是佛可以登录
     *
     * @param phone
     */
    public void verifyCanLogin(String phone) {
        if (TextUtils.isEmpty(phone)) {
            view.onError("手机号不能为空");
            return;
        }
//        if (TextUtils.isEmpty(code)){
//            view.onError("验证码不能为空");
//            return;
//        }
//        if (!TextUtil.isMobileNO(phone)){
//            view.onError("手机号格式不正确");
//            return;
//        }
        view.onLoading();
        Map<String, String> params = new HashMap<>();
        params.put("mobile", phone);
        Map<String, String> header = new HashMap<>();
        header.put("Date", HttpSign.getGMTTime());
        header.put("Source", RetrofitManager.getSource());
        header.put("Authorization", RetrofitManager.getTencentSign());
        RetrofitManager.getDefault().create(ApiServer.class).getIfCanLogin(header, params)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<VerifyCodeBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        compositeDisposable.add(d);
                    }

                    @Override
                    public void onNext(VerifyCodeBean verifyCodeBean) {
                        if (verifyCodeBean.isSuccess()) {
                            view.onSuccess();
                        } else {
                            view.onError(verifyCodeBean.getMsg());
                        }
                        Trace.e("wcj===========" + verifyCodeBean.getMsg());
                    }

                    @Override
                    public void onError(Throwable e) {
                        view.onError(e.getMessage());
                        Trace.e("wcj===========" + e.getMessage());
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    ;

    /**
     * 获取验证码
     *
     * @param phone
     */
    public void getVerifyCode(String phone) {
        if (TextUtils.isEmpty(phone)) {
            view.onError("手机号不能为空");
            return;
        }
        view.onLoading();
        Map<String, String> params = new HashMap<>();
        params.put("mobile", phone);
        params.put("timeStamp", System.currentTimeMillis() / 1000 + "");//加时间戳参数
        params.put("sign", MD5Util.md5(params));//md5加密并加参数
        RetrofitManager.getDefault().create(ApiServer.class).getVerifyCode(params)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<CodeBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        compositeDisposable.add(d);
                    }

                    @Override
                    public void onNext(CodeBean codeBean) {
                        if (codeBean.isSuccess()) {
                            view.onGetSuccess(codeBean);
                            PreferenceUtil.getInstance().commitString(PreferenceUtil.MOBILE_VERIFY, codeBean.getMobile_verify());
                        } else {
                            view.onError(codeBean.getError());
                        }
                        Trace.e("wcj===========" + codeBean.getMobile_verify());
                    }

                    @Override
                    public void onError(Throwable e) {
                        view.onError(e.getMessage());
                        Trace.e("wcj===========" + e.getMessage());
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    /**
     * 登录接口
     *
     * @param phone
     * @param code
     */
    public void toLogin(final String phone, String code) {
        if (TextUtils.isEmpty(phone)) {
            view.onError("手机号不能为空");
            return;
        }
        if (TextUtils.isEmpty(code)) {
            view.onError("验证码不能为空");
            return;
        }
        if (!TextUtil.isMobileNO(phone)) {
            view.onError("手机号格式不正确");
            return;
        }
        if (TextUtil.isMobileNO(PreferenceUtil.getInstance().getString(PreferenceUtil.MOBILE_VERIFY, ""))) {
            view.onError("手机验证不正确");
            return;
        }
        view.onLoading();
        Map<String, String> params = new HashMap<>();
        params.put("mobile", phone);
        params.put("mobilecode", code);
        params.put("verify", PreferenceUtil.getInstance().getString(PreferenceUtil.MOBILE_VERIFY, ""));
        params.put("timeStamp", System.currentTimeMillis() / 1000 + "");//加时间戳参数
        params.put("sign", MD5Util.md5(params));//md5加密并加参数
        RetrofitManager.getDefault().create(ApiServer.class).loginV2(RetrofitManager.getHeader(), params)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<LoginV2Bean>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        compositeDisposable.add(d);
                    }

                    @Override
                    public void onNext(LoginV2Bean loginBean) {
                        if (loginBean.isSuccess()) {
                            PreferenceUtil.getInstance().commitString(PreferenceUtil.MOBILE, phone);
                            view.onLoginSuccess();
                        } else {
                            view.onError(loginBean.getMsg());
                        }
                        Trace.e("wcj===========" + loginBean.getMsg());
                    }

                    @Override
                    public void onError(Throwable e) {
                        view.onError(e.getMessage());
                        Trace.e("wcj===========" + e.getMessage());
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}
